package com.jingfu.util;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jingfu.dto.UserDTO;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author jingfuu@163.com
 * @version v1.0
 * @date 2022/1/31 上午10:58
 **/
public class PageUtil<T> {
    /**
     * 当前记录起始索引
     */
    public static final String PAGE_NUM = "page";

    /**
     * 每页显示记录数
     */
    public static final String PAGE_SIZE = "limit";


    /**
     * 封装分页对象
     */
    public static Map<String,Integer> getPageDomain()
    {
        Map<String, Integer> map = new HashMap<>();
        map.put(PAGE_NUM,ServletUtils.getParameterToInt(PAGE_NUM));
        map.put(PAGE_SIZE,ServletUtils.getParameterToInt(PAGE_SIZE));
        return map;
    }

    public static void startPage() {
        Map<String, Integer> pageDomain = getPageDomain();
        Integer pageNum = pageDomain.get(PAGE_NUM);
        Integer pageSize = pageDomain.get(PAGE_SIZE);
        if (Objects.nonNull(pageNum) && Objects.nonNull(pageSize)) {
            PageHelper.startPage(pageNum, pageSize);
        }
    }

    public static PageResult buidPage(List<?> list) {
        PageResult<List<?>> page = PageResult.page(list, new PageInfo(list).getTotal());
        PageHelper.clearPage();
        return page;
    }
}
